package com.shangyueshang.controller.supply;

import com.shangyueshang.model.Product;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.request.ProductDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.ProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * @author JokeyFeng
 * @date: 2022/1/15 22:36
 * @project: shangyueshang
 * @package: com.shangyueshang.controller.supply
 * @comment:
 */
@RestController
@RequestMapping("/product")
public class ProductController {
	
	@Resource
	private ProductService productService;
	
	/**
	 * 查询所有产品信息
	 *
	 * @return
	 */
	@GetMapping("/findAll")
	@PreAuthorize("hasAnyAuthority('supply:product:view','sale:goods:view')")
	public HttpResult<List<Product>> findAll() {
		List<Product> list = productService.list();
		list.sort(Comparator.comparing(Product::getCreateTime).reversed());
		return HttpResult.ok(list);
	}
	
	/**
	 * 分页查询
	 *
	 * @param pageReq
	 * @return
	 */
	@PostMapping("/findPage")
	@PreAuthorize("hasAuthority('supply:product:view')")
	public HttpResult<PageResult<Product>> findPage(@RequestBody PageReq pageReq) {
		return HttpResult.ok(productService.findPage(pageReq));
	}
	
	/**
	 * 保存产品信息
	 *
	 * @param productDTO
	 * @return
	 */
	@PostMapping("/save")
	@PreAuthorize("hasAnyAuthority('supply:product:add','supply:product:edit')")
	public HttpResult<Boolean> save(@RequestBody ProductDTO productDTO) {
		return productService.saveUpdate(productDTO);
	}
}
